package dao.impl;

import dao.BaseDAO;
import dao.UserDAO;
import pojo.UserDO;

import java.sql.SQLException;


public class UserDAOImpl extends BaseDAO implements UserDAO {
    @Override
    public UserDO getByUsernameAndPwd(String username, String pwd) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
        String sql = "select * from t_users where u_name=? and u_psw=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            statement.setObject(2, pwd);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setAccount(rs.getString("u_name"));
                user.setPsw(rs.getString("u_psw"));
                user.setBalance(rs.getDouble("u_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }

    @Override
    public UserDO getByUsernameAndPwd(String name) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
        String sql = "select * from t_users where u_name=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, name);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setAccount(rs.getString("u_name"));
                user.setPsw(rs.getString("u_psw"));
                user.setBalance(rs.getDouble("u_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }

    @Override
    public void update(UserDO user) {
        this.getConnection();
        String sql = "update t_users set u_psw=?,u_money=? where u_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, user.getPsw());
            statement.setObject(2, user.getBalance());
            statement.setObject(3, user.getId());
            int row = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement);
        }
    }

    @Override
    public UserDO getById(int id) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
        String sql = "select * from t_users where u_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1,id);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setAccount(rs.getString("u_name"));
                user.setPsw(rs.getString("u_psw"));
                user.setBalance(rs.getDouble("u_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }



}
